package com.itaem.community.core.entity;

import com.itaem.community.core.enums.ResultEnum;
import io.swagger.annotations.ApiModel;

import java.io.Serializable;
import java.util.HashMap;

import static com.itaem.community.core.enums.ResultEnum.ERROR;
import static com.itaem.community.core.enums.ResultEnum.OK;

/**
 * <p>项目名称: community </p>
 * <p>文件名称: Result </p>
 * <p>描述: [类型描述] </p>
 * <p>创建时间: 2020/10/11 </p>
 * <p>作者信息: tomato</p>
 *
 * @author tomato
 * @version v1.0
 */
@ApiModel("返回数据包装类")
public class Result extends HashMap<String, Object> implements Serializable {

    @Override
    public Result put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    public Result(ResultEnum resultEnum, Object data) {
        super(3);
        this.put("code", resultEnum.getCode());
        this.put("message", resultEnum.getMessage());
        if (data != null) {
            this.put("data", data);
        }
    }

    public Integer getCode() {
        return (Integer) this.get("code");
    }

    public Result setCode(Integer code) {
        this.put("code", code);
        return this;
    }

    public String getMessage() {
        return (String) this.get("message");
    }

    public Result setMessage(String message) {
        this.put("message", message);
        return this;
    }

    /**
     * 返回2000, OK
     *
     * @return Result stands for success
     */
    public static Result success() {
        return new Result(OK, null);
    }

    public static Result success(Object data) {
        return new Result(OK, data);
    }

    public static Result failure(ResultEnum resultEnum) {
        return new Result(resultEnum, null);
    }

    public static Result failure(ResultEnum resultEnum, Object data) {
        return new Result(resultEnum, data);
    }
}
